package com.im.partchat.widget.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.im.partchat.R;


public class CommomDialog {
    protected final AlertDialog.Builder builder;
    protected final Context context;
    protected LinearLayout rootView;
    protected TextView tvTitle;
    protected Button btnClose;
    protected Button btnSave;
    protected AlertDialog dialog;
    protected float density;

    public CommomDialog(Context context) {
        this.context = context;
        density = context.getResources().getDisplayMetrics().density;
        builder = new AlertDialog.Builder(context);
        initView();
        builder.setView(rootView);
    }

    protected void initView() {
        rootView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.comm_dialog, null);
        tvTitle = (TextView) rootView.findViewById(R.id.tv_video_dialog_title);
        btnClose = (Button) rootView.findViewById(R.id.btn_video_dialog_close);
        btnSave = (Button) rootView.findViewById(R.id.btn_video_dialog_save);
        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public CommomDialog setNegativeOnClickListener(final View.OnClickListener negativeOnClickListener) {
        if (negativeOnClickListener == null) {
            return this;
        }

        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                negativeOnClickListener.onClick(btnClose);
                dismiss();
            }
        });
        return this;
    }



    public CommomDialog setPositiveOnClickListener(final View.OnClickListener positiveOnClickListener) {
        if (positiveOnClickListener == null) {
            return this;
        }


        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                positiveOnClickListener.onClick(btnSave);
                dismiss();
            }
        });
        return this;
    }

    public CommomDialog setTitle(String title) {
        if (title == null) {
            title = "";
        }
        tvTitle.setText(title);
        return this;
    }

    public CommomDialog setContent(@LayoutRes int contentId) {
        View view = LayoutInflater.from(context).inflate(contentId, null);
        return setContent(view);
    }

    public CommomDialog setContent(View view){
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rootView.addView(view, 2, params);
        return this;
    }

    public void show() {
        if (dialog == null) {
            dialog = builder.create();
        }
        dialog.show();
    }

    public void dismiss() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }
}
